<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Hint;
use App\Http\Model\UserAgent;
use App\Http\Model\UserAgentGolod;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\FinanceAgentj;
use App\Http\Model\FinanceJd;

/**
 * 用户代理
 */
class UserAgentServices extends CommonServices
{
    private $userAgentModel;
    private $usergolodAgentModel;
    private $userWalletModel;
    private $financeAgentjModel;
    private $financeJdModel;

    public function __construct()
    {
        $this->userAgentModel = new UserAgent();
        $this->usergolodAgentModel = new UserAgentGolod();
        $this->userWalletModel = new FinanceUserWallet();
        $this->financeAgentjModel = new FinanceAgentj();
        $this->financeJdModel = new FinanceJd();

    }

    /**
     * 新增代理用户
     */
    public function agentUserAdd($userId, $rate)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->userAgentModel->getUserAgent($userId);
            if(!empty($info)) {
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }
            $id = $this->userAgentModel->agentUserAdd($userId, $rate);
            if(empty($id)) {
                $code = Hint::ERR_SYS_FAIL;
                break;
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /**
     * 删除代理用户
     */
    public function agentUserDel($userId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->userAgentModel->getUserAgent($userId);
            if(empty($info)) {
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }

            //获取用户账户
            $wallet = $this->userWalletModel->get_wallet($userId);
            $config = config('pay');
            $agent_desc = $config['wallet_change_desc']['agentj_secure'];
            $jd_desc = $config['wallet_change_desc']['jd_agentj_secure'];

            DB::beginTransaction();
            try {
                $this->userAgentModel->agentUserDel($userId);

                DB::commit();
            } catch (\Exception $e) {
                echo $e->getMessage();

                $code = Hint::ERR_SYS_FAIL;
                DB::rollBack();
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /**
     * 编辑代理用户
     */
    public function agentUserEdit($userId, $rate)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->userAgentModel->getUserAgent($userId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                $msg = '用户不是代理';
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }
            $id = $this->userAgentModel->agentUserEdit($userId, $rate);
            if(empty($id)) {
                $code = Hint::ERR_SYS_FAIL;
                break;
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /**
     * 新增金币代理用户
     */
    public function agentGolodUserAdd($userId, $rate)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->usergolodAgentModel->getUserAgent($userId);
            if(!empty($info)) {
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }
            $id = $this->usergolodAgentModel->agentUserAdd($userId, $rate);
            if(empty($id)) {
                $code = Hint::ERR_SYS_FAIL;
                break;
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /**
     * 删除金币代理用户
     */
    public function agentGolodUserDel($userId)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->usergolodAgentModel->getUserAgent($userId);
            if(empty($info)) {
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }

            //获取用户账户
            $wallet = $this->userWalletModel->get_wallet($userId);
            $config = config('pay');
            $agent_desc = $config['wallet_change_desc']['agentj_secure'];
            $jd_desc = $config['wallet_change_desc']['jd_agentj_secure'];

            DB::beginTransaction();
            try {
                $this->usergolodAgentModel->agentUserDel($userId);

                DB::commit();
            } catch (\Exception $e) {
                echo $e->getMessage();

                $code = Hint::ERR_SYS_FAIL;
                DB::rollBack();
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /**
     * 编辑金币代理用户
     */
    public function agentGolodUserEdit($userId, $rate)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            if(empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->usergolodAgentModel->getUserAgent($userId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                $msg = '用户不是代理';
                break;
            }
            //暂停代理操作
            $cacheKey = 'USER:AGENT:'.$userId;
            if (!redis_lock($cacheKey, 10)) {
                $code = Hint::SAME_TIME_NOT_REPEAT;
                break;
            }
            $id = $this->usergolodAgentModel->agentUserEdit($userId, $rate);
            if(empty($id)) {
                $code = Hint::ERR_SYS_FAIL;
                break;
            }
            redis_unlock($cacheKey);

        } while (false);
        return compact('code', 'msg', 'data');
    }

}